﻿using UnityEngine;
using System.Collections;

public class ForcesDeploySpot : MonoBehaviour {

	public int nLine;
	public int index;
	public GameObject spotFX;
	public int mNo;
	public GameObject pawnGo;
	public bool isDeployed;

	public void Deploy(int no, GameObject go)
	{
		Debug.Log("Deploy  "+no);
		if(ForcesDeploy.Instance.isDeployedDic.ContainsKey(no) )
			ForcesDeploy.Instance.isDeployedDic[no] = true;
		else
			ForcesDeploy.Instance.isDeployedDic.Add ( no, true);
		mNo = no;
		pawnGo = go;
		isDeployed=true;
		pawnGo.SetActive(true);
		spotFX.SetActive(false);
		if(ForcesDeploy.Instance.selectedMember.no == mNo)
			ForcesDeploy.Instance.ShowSelectedMemberFX(mNo, pawnGo, true);
	}

	public void UnDeploy()
	{
		Debug.Log("UnDeploy  "+mNo);
		if(ForcesDeploy.Instance.isDeployedDic.ContainsKey(mNo) )
			ForcesDeploy.Instance.isDeployedDic[mNo] = false;
		else
			ForcesDeploy.Instance.isDeployedDic.Add ( mNo, false);
		ForcesDeploy.Instance.ShowSelectedMemberFX(mNo, pawnGo, false);
		isDeployed = false;
		if(pawnGo!=null)
			pawnGo.SetActive(false);
		ForcesDeployScene scene = ForcesDeployScene.Instance as ForcesDeployScene;
		spotFX.SetActive(scene.isModifyMode);
		mNo=0;
	}

	void OnMouseDown()
	{
		ForcesDeployScene scene = ForcesDeployScene.Instance as ForcesDeployScene;
		Debug.Log(mNo+"  onMouseDown");
		if(!scene.isModifyMode) {
			if(isDeployed) {
				scene.SetSelectMemberOnLeftUI(mNo);
			}
			return;
		}
		ForcesDeploySpot selectedMemberSpot = ForcesDeploy.Instance.FindSelectedMemberDeployedSpot();
		if(isDeployed) {
			if(selectedMemberSpot==null)//첫 배치이면 배치되어있던 애는 다시 리스트로 가야한다//
			{
				scene.SetDeployedCheck(mNo, false);
				UnDeploy();
			}
			else// 서로 위치를 바꾼다//
				ForcesDeploy.Instance.Deploy (mNo, 0, selectedMemberSpot.index);
			ForcesDeploy.Instance.DeploySelectedMember(index);
		}
		else {
			if(ForcesDeploy.Instance.IsDeployedMax() && selectedMemberSpot == null)//꽉 찬 상태에서 신규 배치 못하도록//
			{}
			else
				ForcesDeploy.Instance.DeploySelectedMember(index);
		}
	}

}
